<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

<script>
    let raceArr = [
        new Promise((resolve, reject) => {
            setTimeout(_ => {
                console.log("3s");
                resolve('race-----3s');
            }, 3000);
        }),
        new Promise((resolve, reject) => {
            setTimeout(_ => {
                console.log("2s");
                resolve('race-----2s');
            }, 2000);
        }),
        new Promise((resolve, reject) => {
            setTimeout(_ => {
                console.log("5s");
                resolve('race-----5s');
            }, 5000);
        }),
    ];

    // function promiseRace(promiseArr) {
    //     let arrLen = promiseArr.length;
    //     let status = false;
    //     return new Promise((resolve, reject) => {
    //         try {
    //             promiseArr.forEach((p, index) => {
    //                 console.log(status);
    //                 if (!status) {
    //                     p.then(res => {
    //                         resolve(res);
    //                         status = true;
    //                     }, err => {
    //                         reject(err);
    //                     })
    //                 }
    //             })
    //         } catch(err) {
    //             reject(err);
    //         }
    //     });
    // }

    // 请求某个图片资源
    function requestImg() {
        let p = new Promise((resolve, reject) => {
            var img = new Image();
            img.onload = function() {
                resolve(img);
            }
            img.src = '../../images/流程图.png'
        })

        return p;
    }

    // 延时函数，用于给请求计时
    function timeout() {
        var p = new Promise((resolve, reject) => {
            setTimeout(function() {
                reject("请求超时");
            }, 5000);
        });

        return p;
    }

    // Promise.race([requestImg(), timeout()])
    // .then(res => {
    //     console.log(res);
    // })
    // .catch(err => {
    //     console.log(err);
    // }) 

    function promiseRace(arr) {
        if (!Array.isArray(arr)) {
            throw new Err('no array!');
        }
        return new Promise((resolve, reject) => {
            arr.forEach(p => {
                Promise.resolve(p).then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                })
            })
        })
    }

    function promiseRace(arr) {
        if (!Array.isArray(arr)) {
            throw new Err('no array!');
        }
        return new Promise((resolve, reject) => {
            arr.forEach(p => {
                Promise.resolve(p).then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                })
            })
        })
    }

    promiseRace(raceArr);

</script>

</html>